package com.module.dialog.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.module.dialog.CustomListDialog;
import com.module.dialog.inter.DialogOnItemListener;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

/**
 * Created by dell on 2018/7/6
 */
public abstract class CustomAdapter<T> extends RecyclerView.Adapter {

    private Context mContext;
    private CustomListDialog.ListParam mListParam;
    private DialogOnItemListener onItemListener;


    public CustomAdapter(Context context) {
        this.mContext = context;
    }
    public void setOnItemListener(DialogOnItemListener onItemListener) {
        this.onItemListener = onItemListener;
    }

    public void setListParam(CustomListDialog.ListParam param) {
        this.mListParam = param;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        //解决item宽度不能铺满
        View view = LayoutInflater.from(mContext).inflate(getLayoutRes(), viewGroup, false);
        return new CustomViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        bindData((CustomViewHolder)holder, position, mListParam.items);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemListener != null) {
                    onItemListener.onItemClick(position);
                }
            }
        });
    }


    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return mListParam.items.size();
    }

    public class CustomViewHolder extends RecyclerView.ViewHolder {

        public CustomViewHolder(@NonNull View view) {
            super(view);
        }
    }

    public abstract int getLayoutRes();
    public abstract void bindData(CustomViewHolder holder, int position, List<T> datas);

}